import sys
import getopt

#http://pl.wikibooks.org/wiki/Zanurkuj_w_Pythonie/Obs%C5%82uga_argument%C3%B3w_linii_polece%C5%84

#args examples:

# C:\Users\MarcinW\Desktop\Python_Subversion\trunk\2.7.9\Inne>python getOPT.py -h
# -g arg -d
# [('-h', ''), ('-g', 'arg'), ('-d', '')]
# []
# default help

# C:\Users\MarcinW\Desktop\Python_Subversion\trunk\2.7.9\Inne>python getOPT.py -h
# -g arg -d arg_not -h -d
# [('-h', ''), ('-g', 'arg'), ('-d', '')]
# ['arg_not', '-h', '-d']
# default help

# C:\Users\MarcinW\Desktop\Python_Subversion\trunk\2.7.9\Inne>python getOPT.py -h
# -g arg -d arg_not -h -d
# [('-h', ''), ('-g', 'arg'), ('-d', '')]
# ['arg_not', '-h', '-d']
# default help

def main(argv):
              
    try:
        #if we have for example g: we have the arg with value
        opts, args = getopt.getopt(argv, "hg:d", ["help=", "grammar="])
        #opts is argument with getOPT
        print (opts)
        #arg <=> sys.argv
        print (args)
    # we have custom error class here => getopt.GetoptError Module.Class
    except getopt.GetoptError, e:
        print e
        sys.exit(2)                     
    for opt, arg in opts:                
        if opt in ("-h", "--help"):      
            print ("default help")
            sys.exit()                  
        elif opt == '-d':                
            global _debug               
            _debug = 1                  
        elif opt in ("-g", "--grammar"):
            grammar = arg

call = main(sys.argv[1:])

